package com.mz.ozy.activity.me;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.Html;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.activity.assets.TbRecordActivity;
import com.mz.ozy.base.BaseActivity;
import com.mz.ozy.bean.NewTbBean;
import com.mz.ozy.bean.TbInfoBean;
import com.mz.ozy.mvp.presenter.TCoinsPresenter;
import com.mz.ozy.mvp.view.TCoinsContract;
import com.mz.ozy.utils.MD5Util;
import com.mz.ozy.utils.ToastUtils;
import com.mz.ozy.utils.Utils;
import com.mz.ozy.widget.dialog.BaseDialog;
import com.mz.ozy.widget.dialog.PayPassDialog;
import com.mz.ozy.widget.dialog.PayPassView;
import com.mz.ozy.widget.dialog.PayPopupWindow;

import org.jetbrains.annotations.NotNull;

import java.math.BigDecimal;

import androidx.annotation.Nullable;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @author lifeStyle on 2020/4/2 0002.
 */
public class MeNewWithActivity extends BaseActivity<TCoinsContract.View, TCoinsPresenter> implements View.OnClickListener, TCoinsContract.View {
    public static final int PAY_TYPE_WX = 1;
    public static final int PAY_TYPE_ALIPAY = 0;
    public static final int PAY_TYPE_BANK = 2;
//    @BindView(R.id.tv_bind_wechat)
//    TextView tvBindWechat;
//    @BindView(R.id.tv_bind_ali)
//    TextView tvBindAli;
//    @BindView(R.id.tv_bind_bank)
//    TextView tvBindBank;

    @BindView(R.id.iv_wx_select)
    ImageView ivWxSelect;
    @BindView(R.id.iv_alipay_select)
    ImageView ivAlipaySelect;
    @BindView(R.id.iv_bank_select)
    ImageView ivBankSelect;

    @BindView(R.id.tv_balance)
    TextView tvBalance;
    @BindView(R.id.et_money)
    EditText etMoney;
    @BindView(R.id.et_account_no)
    EditText etAccountNo;
    @BindView(R.id.et_account_name)
    EditText etAccountName;
    @BindView(R.id.tv_all_comit)
    TextView tvAllComit;

    @BindView(R.id.btn_comit)
    Button btnComit;

    private int type = PAY_TYPE_ALIPAY;
    private String remain = null;

    private TextWatcher mTextWatcherBAA = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

        }

        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {


        }

        @Override
        public void afterTextChanged(Editable editable) {
            //   Utils.editTextUtils(editable, etMoney);
//            if (TextUtils.isEmpty(editable.toString()) || editable.toString().equals(".") || Double.parseDouble(editable.toString()) == 0) {
//                tvReallDz.setText("实际到账: 0.00");
//                tvFee.setText("手续费: 0.00");
//            } else {
//                if (Double.parseDouble(editable.toString()) > user_balance) {
//                    setEtMoney();
//                } else {
//                    tvReallDz.setText("实际到账: " + Utils.decimalUtils(String.valueOf(new BigDecimal(editable.toString()).subtract(new BigDecimal(dataItem.getWithdrawal_fee()).multiply(new BigDecimal(editable.toString())))), 2));
//                    tvFee.setText("手续费: " + Utils.decimalUtils(Double.parseDouble(editable.toString()) * Double.valueOf(dataItem.getWithdrawal_fee()) + "", 2));
//                }
//            }
        }
    };

    private PayPopupWindow mPayPopupWindow;
    private String accoun;
    private PayPassDialog dialog;
    private double user_balance;


    public static void go(Context context, String remain) {
        Intent intent = new Intent(context, MeNewWithActivity.class);
        intent.putExtra("remain", remain);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_me_new_with);
        ButterKnife.bind(this);

        remain = getIntent().getStringExtra("remain");
        tvBalance.setText(remain);

        initTitle();
        tvCenterTitle.setText("奖金兑换");
        rightImage.setVisibility(View.VISIBLE);
        rightImage.setImageResource(R.mipmap.me_with_recode);
        rightImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(MeNewWithActivity.this, TbRecordActivity.class));
            }
        });
        etMoney.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        etMoney.addTextChangedListener(mTextWatcherBAA);

//        mPresenter.getTbInfo(this);
    }


    @OnClick({R.id.ll_bank, R.id.ll_wx, R.id.ll_alipay, R.id.btn_comit, R.id.tv_all_comit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_bind_wechat:
//                if (dataItem.getWechatnum() == 0) {
//                    ReceiveAccountActivity.go(MeNewWithActivity.this, 1);
//                }
                break;
            case R.id.tv_bind_ali:
//                if (dataItem.getAlipaynum() == 0) {
//                    ReceiveAccountActivity.go(MeNewWithActivity.this, 2);
//                }
                break;
            case R.id.tv_bind_bank:

                break;

            case R.id.ll_bank:
                type = PAY_TYPE_BANK;
                updateSelectStatus();
                //ReceiveAccountActivity.go(MeWithActivity.this, 1);
                break;
            case R.id.ll_wx:
                type = PAY_TYPE_WX;
                updateSelectStatus();
                //ReceiveAccountActivity.go(MeWithActivity.this, 1);
                break;
            case R.id.ll_alipay:
                type = PAY_TYPE_ALIPAY;
                updateSelectStatus();
                break;
            case R.id.btn_comit:
                //提交
                String money = etMoney.getText().toString();
                String account = etAccountNo.getText().toString();
                String name = etAccountName.getText().toString();
//                if (TextUtils.isEmpty(money) || (!TextUtils.isEmpty(dataItem.getWithdrawal_min())) && Double.parseDouble(money) < Double.parseDouble(dataItem.getWithdrawal_min() + "")) {
//                    ToastUtils.showToast(String.format(getString(R.string.str_transfer_num_hint), dataItem.getWithdrawal_min() + ""));
//                    return;
//                }

                if (TextUtils.isEmpty(money)){
                    ToastUtils.showToast(etMoney.getHint().toString());
                }

                if (TextUtils.isEmpty(account)){
                    ToastUtils.showToast(etAccountNo.getHint().toString());
                }

                if (TextUtils.isEmpty(name)){
                    ToastUtils.showToast(etAccountName.getHint().toString());
                }

                if (type != PAY_TYPE_ALIPAY && type != PAY_TYPE_WX & type != PAY_TYPE_BANK) {
                    ToastUtils.showToast("请选择支付方式");
                    return;
                }

//                dialog = new BaseDialog(MeNewWithActivity.this, R.layout.dialog_private, Gravity.CENTER, true);
//                ((TextView) dialog.getView(R.id.tv_dialog_content)).setText("确定提现"+ money +"奖励?");
//                ((TextView) dialog.getView(R.id.tv_dialog_title)).setText("提示");
//                ((TextView) dialog.getView(R.id.btn_confirm)).setText("确定");
//                dialog.getView(R.id.btn_cancel).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        dialog.toggleDialog();
//                    }
//                });
//                dialog.getView(R.id.btn_confirm).setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        mPresenter.newOnTb(MeNewWithActivity.this, money, name, account, type + "");
//                        dialog.toggleDialog();
//                    }
//                });
//                dialog.toggleDialog();


//                if (type == PAY_TYPE_ALIPAY && dataItem.getAlipaynum() == 0) {
//                    ToastUtils.showToast("请绑定支付宝");
//                    return;
//                }
//                if (type == PAY_TYPE_WX && dataItem.getWechatnum() == 0) {
//                    ToastUtils.showToast("请绑定微信");
//                    return;
//                }
//                if (type == PAY_TYPE_BANK && dataItem.getBanknum() == 0) {
//                    ToastUtils.showToast("请绑定银行");
//                    return;
//                }

                dialog = new PayPassDialog(this);
                dialog.getPayViewPass().setForgetText(money);
                dialog.getPayViewPass().setHintText("确认提现");
                dialog.getPayViewPass()
                        .setPayClickListener(new PayPassView.OnPayClickListener() {
                            @Override
                            public void onPassFinish(String passContent) {
                                //6位输入完成回调
//                                mPresenter.onTb(MeWithActivity.this, type + "", accoun, money, MD5Util.getMD5String(passContent));
                                //String amount, String name, String account, String type
                                mPresenter.newOnTb(MeNewWithActivity.this, money, name, account, type + "", MD5Util.getMD5String(passContent));
                                dialog.dismiss();
                            }

                            @Override
                            public void onPayClose() {
                                dialog.dismiss();
                                //关闭回调
                            }

                            @Override
                            public void onPayForget() {
                                dialog.dismiss();
                                //点击忘记密码回调
                                startActivity(new Intent(MeNewWithActivity.this, PayPwdActivity.class));
                            }
                        });

                /*if (mPayPopupWindow == null) {
                    mPayPopupWindow = new PayPopupWindow(this, new PayPopupWindow.OnClickListener() {
                        @Override
                        public void onConfirm(String pwd) {
                            mPresenter.onTb(MeWithActivity.this, type + "", accoun, money, MD5Util.getMD5String(pwd));

                        }
                    });
                }
                mPayPopupWindow.showAtLocation(titleBar, Gravity.BOTTOM, 0, 0);*/

                break;
            case R.id.tv_all_comit:
                //提交
                setEtMoney();
                break;
        }
    }

    private void setEtMoney() {
        etMoney.removeTextChangedListener(mTextWatcherBAA);
        etMoney.setText(remain);
        etMoney.setSelection(etMoney.getText().toString().trim().length());
//        tvReallDz.setText("实际到账: " + Utils.decimalUtils(String.valueOf(new BigDecimal(dataItem.getUser_balance()).subtract(new BigDecimal(dataItem.getWithdrawal_fee()).multiply(new BigDecimal(dataItem.getUser_balance())))), 2));
//        tvFee.setText("手续费: " + Utils.decimalUtils(Double.parseDouble(dataItem.getUser_balance()) * Double.valueOf(dataItem.getWithdrawal_fee()) + "", 2));
//        etMoney.addTextChangedListener(mTextWatcherBAA);
    }

    private void updateSelectStatus() {

        ivWxSelect.setImageResource(R.mipmap.cb_red_normal);
        ivAlipaySelect.setImageResource(R.mipmap.cb_red_normal);
        ivBankSelect.setImageResource(R.mipmap.cb_red_normal);
        if (type == PAY_TYPE_WX) {
//            accoun = dataItem.getWechat();
            ivWxSelect.setImageResource(R.mipmap.cb_red_select);
        } else if (type == PAY_TYPE_ALIPAY) {
//            accoun = dataItem.getAlipay();
            ivAlipaySelect.setImageResource(R.mipmap.cb_red_select);
        } else {
//            accoun = dataItem.getBank_account();
            ivBankSelect.setImageResource(R.mipmap.cb_red_select);
        }
    }

    @Override
    public void onClick(View v) {

    }

    @Override
    public void getCodeSuccess(Object o) {

    }

    @Override
    public void getTbInfoSuccess(TbInfoBean bean) {
//        dataItem = bean;
        String fee = Utils.subZeroAndDot(Double.parseDouble(bean.getWithdrawal_fee()) * 100 + "") + "%";
//        tvWithFee.setText(Html.fromHtml(bean.getTxsm()));
        user_balance = Double.parseDouble(bean.getUser_balance());
        tvBalance.setText(Utils.decimalUtils(bean.getUser_balance(), 2));
//        if (bean.getWechatnum() == 0) {
//            tvBindWechat.setText("去绑定");
//        } else {
//            tvBindWechat.setText(bean.getWechat());
//            tvBindWechat.setTextColor(getResources().getColor(R.color.c_a5a5a5));
//        }
//        if (bean.getAlipaynum() == 0) {
//            tvBindAli.setText("去绑定");
//        } else {
//            tvBindAli.setText(bean.getAlipay());
//            tvBindAli.setTextColor(getResources().getColor(R.color.c_a5a5a5));
//        }
//        if (bean.getBanknum() == 0) {
//            tvBindBank.setText("去绑定");
//        } else {
//            tvBindBank.setText(bean.getBank_name() + bean.getBank_account());
//            tvBindBank.setTextColor(getResources().getColor(R.color.c_a5a5a5));
//        }
//        etMoney.setHint(String.format(getString(R.string.str_transfer_num_hint), dataItem.getWithdrawal_min() + ""));
//        updateSelectStatus();
    }

    @NotNull
    private String getText(TbInfoBean bean, String fee) {
        return "1、每月" + bean.getWithdrawal_date() + "号以后可以提现。\n2、提现" + bean.getWithdrawal_min() + "元起，收取" + fee + "手续费。";
    }

    @Override
    public void onTbSuccess(Object o) {
        dialog.dismiss();
        ToastUtils.showToast("提现成功");
        startActivity(new Intent(this, TbRecordActivity.class));
        finish();
        validateRequestPermissionsRequestCode(10);
    }

    @Override
    public void newOnTbSuccess(NewTbBean o) {
        dialog.dismiss();
        ToastUtils.showToast(o.msg);
        startActivity(new Intent(this, TbRecordActivity.class));
        finish();
        validateRequestPermissionsRequestCode(10);
    }

    @Override
    protected void onResume() {
        super.onResume();
//        mPresenter.getTbInfo(this);
    }
}
